var do_App = sm("do_App");
var do_Page = sm("do_Page");
var nf = sm("do_Notification");
var communityUrl = require("url/community");
var circleUrl = require("url/circle");
var communityUtil = require("helper/community");
var userUtil = require("helper/user");
var httpSilence = require("helper/httpSilence");
var openHelper = require("helper/open");
var qqHttp = require("helper/http");
var bdmapHelper = require("helper/bdmap");
var urlUser = require("url/user");
// 社区头部
var layoutHead = ui("layoutHead");
var mainHeadAdd = layoutHead.add("mainHead","source://view/community/index/header/mainHeader.ui", 0, 0);

var mainHead = ui("mainHead");
var headerTitle = ui(mainHeadAdd + ".headerTitle");
var titleBorderView = ui(mainHeadAdd + ".titleBorderView");
var aLayoutRoot=ui("aLayoutRoot");

var listdata = mm("do_ListData");
var listView = ui("listView");
listView.bindItems(listdata);

laodPage();
var page,tagList,tabType=2;
var isLoadFirst=false;
var isMessageType=true;
var communityId;

var topCellUi = ui(aLayoutRoot.add("idTopCell", "source://view/community/index/indexCell06.ui", 0, 75));
topCellUi.visible = false;
var isCommunityMessage=false;
var dingweiX, dingweiY;


function laodPage() {
	httpSilence.post(urlUser.url.base.getUserBaseConfig, {type:2}, function(data) {
		var interestData = '[';
	    for(var i=0;i<data.data.length;i++)
	    	{
	    	var obj='{"val":"'+data.data[i].id+'","name":"'+data.data[i].name+'"}';
	    	interestData += obj;
	    	if(i!=data.data.length-1)
	    	    interestData += ',';
	    	}
		interestData += ']';
	    //write to local storage

	    userUtil.saveUserPara(1,'interest',interestData);
	    
	});
	httpSilence.post(urlUser.url.base.getUserBaseConfig, {type:1}, function(data) {
		var industryData = '[';
		    for(var i=0;i<data.data.length;i++)
		    	{
		    	var obj='{"val":"'+data.data[i].id+'","name":"'+data.data[i].name+'"}';
		    	industryData += obj;
		    	if(i!=data.data.length-1)
		    		industryData += ',';
		    	}
		industryData += ']';
		//write to local storage
	    userUtil.saveUserPara(1,'industry',industryData);
	});
	httpSilence.post(urlUser.url.base.getUserBaseConfig, {type:3}, function(data) {
		var delicacyData = '[';
		    for(var i=0;i<data.data.length;i++)
		    	{
		    	var obj='{"val":"'+data.data[i].id+'","name":"'+data.data[i].name+'"}';
		    	delicacyData += obj;
		    	if(i!=data.data.length-1)
		    		delicacyData += ',';
		    	}
		delicacyData += ']';
		//write to local storage
	    userUtil.saveUserPara(1,'delicacy',delicacyData);
	});
	httpSilence.post(urlUser.url.base.getUserBaseConfig, {type:4}, function(data) {
		var hometownData = '[';
		    for(var i=0;i<data.data.length;i++)
		    	{
		    	var obj='{"val":"'+data.data[i].id+'","name":"'+data.data[i].name+'"}';
		    	hometownData += obj;
		    	if(i!=data.data.length-1)
		    		hometownData += ',';
		    	}
		hometownData += ']';
		//write to local storage
	    userUtil.saveUserPara(1,'hometown',hometownData);
	});
	httpSilence.post(urlUser.url.base.getUserBaseConfig, {type:5}, function(data) {
		var entertainmentData = '[';
		    for(var i=0;i<data.data.length;i++)
		    	{
		    	var obj='{"val":"'+data.data[i].id+'","name":"'+data.data[i].name+'"}';
		    	entertainmentData += obj;
		    	if(i!=data.data.length-1)
		    		entertainmentData += ',';
		    	}
		entertainmentData += ']'; 
		//write to local storage
	    userUtil.saveUserPara(1,'entertainment',entertainmentData);
	});
	dingweiX=bdmapHelper.getDingwei().longitude;
	dingweiY=bdmapHelper.getDingwei().latitude;
	isLoadFirst=false;
	page = 1;
	var url = communityUrl.url.base.indexNew;
	var userId = userUtil.userId();
	communityId = communityUtil.communityVisitId();
	
	var lastDate = userUtil.loadUserPara(userId,'lastAccessDate-'+communityId);
	if(lastDate==undefined || lastDate== "")
		{
		lastDate = parseInt(new Date().getTime()/1000).toString();
		userUtil.saveUserPara(userId,'lastAccessDate-'+communityId,lastDate);		
		}
	var currentDate = parseInt(new Date().getTime()/1000).toString();
	var param = {};
	if(userId == undefined || userId == "")
	{
		param = {
				"communityId" : communityId,
				"dingweiX" : dingweiX,
				"dingweiY" : dingweiY,				
			}
	}
	else {
		param = {
				"communityId" : communityId,
				"dingweiX" : dingweiX,
				"dingweiY" : dingweiY,
				"lastClick": lastDate,
				"currentClick": currentDate
			}
		
	}
	httpSilence.post(url, param, function(data) {
		if(!(userId == undefined || userId == "")){
		userUtil.saveUserPara(userId,'lastQueryDate-'+communityId,lastDate);
		userUtil.saveUserPara(userId,'currentQueryDate-'+communityId,currentDate);
		}
		listdata.removeAll();
		listView.refreshItems();
		
		communityId=communityUtil.communityVisitId();
		/* borderView标题赋值 */
		var dataTag = {};
		dataTag.name = data.communityBaseVo.name;
		if(data.communityBaseVo.txtShow){
			communityUtil.setJoinCommunityState(0);
			dataTag.text="未加入";
		}else{
			communityUtil.setJoinCommunityState(1);
			dataTag.text="";
		}
		

		do_Page.fire("refreshButton");
		

		titleBorderView.items={"centerView":{"labelTitle":dataTag.name,"labelTitleTxt":dataTag.text}};
		communityUtil.communityVisitId(data.communityBaseVo.id);
		var noticeContent=[{"content":"暂无最新公告"}];
		var noticeTime="";
		if(null!=data.noticeList&&data.noticeList.length>0){
			noticeContent=data.noticeList;
		}
		var showCanJoin=false;
		if(data.showCanJoinSign=="1"){//加入社区
			showCanJoin=true;
		}else{//签到
			showCanJoin=false;
		}
		listdata.addData([{
			"template" : 0,
			"crollbannerList":data.crollbannerList,
			"communityName":data.communityBaseVo.name,
			"qShopCount":data.qShopCount,
			"circleCount":data.circleCount,
			"neighborCount":data.neighborCount,
			"newNeighborShow":(data.newNeighborCount!= 0 && data.newNeighborCount != '' && data.newNeighborCount!=undefined),
			"newNeighborCount":data.newNeighborCount,
			"noticeContent":noticeContent,
			"activityList":data.activityList,
			"circleBaseList":data.circleBaseList,
			"showCanJoin":showCanJoin,
			"curUserCommunityId":data.curUserCommunityId
		}]);		
		tagList=data.tagList;
		tagList[0].tagFontColor="ff5305FF";
		if(tabType==1){
			listdata.addData([{
				"template" : 1,
				"communityMessageTags":tagList,
				"isCommunityMessage":true
			}]);
			getCommunityMessage();
		}else if(tabType==2){
			listdata.addData([{
				"template" : 1,
				"communityMessageTags":tagList,
				"isCommunityMessage":false
			}]);
			getCircle();
		}
		do_Page.fire("initTopCell", {"tagList":tagList,isCommunityMessage:isCommunityMessage});
		do_Page.fire("community_update_isNewsLoaded");
		var date=new Date();
		communityUtil.communityMessageTimeUp(date.getTime());
		
	})
}
headerTitle.on("touch",function(){
	openHelper.open("source://view/community/distance/main.ui");
})
var noCommunityMessageData = [ {
	"template" : 5
} ];
var tagId="-1";
var clearData=false;
function getCommunityMessage(){
	page=1;
	isLoadFirst=false;
	
	
	var messageUrl = communityUrl.url.message.newPage;
	var param = {
		"page" : page,
		"communityId" : communityUtil.communityVisitId(),
		"tagId" : tagId,
		"startAt":communityUtil.communityMessageTimeUp(),
		
	}
	httpSilence.post(messageUrl, param, function(data) {
		
		listView.rebound();
		if(!isMessageType){
			var tabTypeData=listdata.getOne(1);
			tabTypeData.isCommunityMessage=true;
			listdata.updateOne(1, tabTypeData);
			isMessageType=true;
			do_Page.fire("initTopCell", {"tagList":tagList,isCommunityMessage:true});
		}
		if(clearData){
			if(listdata.getCount()>2){
				listdata.removeRange(2, listdata.getCount()-1);
			}
		}
		var mContent = data.page.content;
		if (mContent.length>0) {
			for (var i = 0,len = mContent.length; i < len; i++) {
				mContent[i].template=2;
			}
			listdata.addData(mContent);
		} else {
			if(page==1){
				listdata.addData(noCommunityMessageData);
			}
		}
		listView.refreshItems();
		isLoadFirst=true;
	});
}
function getNextPageCommunityMessage(){
	page++;
	var messageUrl = communityUrl.url.message.newPage;
	var param = {
		"page" : page,
		"communityId" : communityUtil.communityVisitId(),
		"tagId" : tagId,
		"startAt":communityUtil.communityMessageTimeUp()
	}
	qqHttp.post(messageUrl, param, function(data) {
		if (data.page.content.length>0) {
			for (var i = 0; i < data.page.content.length; i++) {
				data.page.content[i].template=2;
			}
			listdata.addData(data.page.content);
		} else {
			if(page==1){
				listdata.addData(noCommunityMessageData);
			}
		}
		listView.refreshItems();
	});
}
function getCircle(){
	page=1;
	isLoadFirst=false;
	var circleTopUrl = circleUrl.url.topic.listCicleTopic4Community;
	var param = {
		"page" : page,
		"communityId" : communityUtil.communityVisitId()
	}
	httpSilence.post(circleTopUrl, param, function(data) {
		listView.rebound();
		if(isMessageType){
			var tabTypeData=listdata.getOne(1);
			tabTypeData.isCommunityMessage=false;
			listdata.updateOne(1, tabTypeData);
			isMessageType=false;
			do_Page.fire("initTopCell", {"tagList":tagList,isCommunityMessage:false});
		}
		if(clearData){
			if(listdata.getCount()>2){
				listdata.removeRange(2, listdata.getCount()-1);
			}
		}
		var activityBaseVo = data.activityBaseVo;
		if(activityBaseVo){
			activityBaseVo.template =3;//指定模板
			listdata.addOne(activityBaseVo);
		}
		if (data.topicPage&&data.topicPage.content.length>0) {
			var tContent = data.topicPage.content;
			for (var i = 0,length = tContent.length; i < length; i++) {
				tContent[i].template=4;
			}
			listdata.addData(tContent);
		} else {
			if(page==1){
				listdata.addData(noCommunityMessageData);
			}
		}
		listView.refreshItems();
		isLoadFirst=true;
	});
}
function getNextPageCircle(){
	page++;
	var circleTopUrl = circleUrl.url.topic.listCicleTopic4Community;
	var param = {
		"page" : page,
		"communityId" : communityUtil.communityVisitId()
	}
	qqHttp.post(circleTopUrl, param, function(data) {
		if (data.topicPage&&data.topicPage.content.length>0) {
			for (var i = 0; i < data.topicPage.content.length; i++) {
				data.topicPage.content[i].template=4;
			}
			listdata.addData(data.topicPage.content);
		} else {
			if(page==1){
				listdata.addData(noCommunityMessageData);
			}
		}
		listView.refreshItems();
	});
}
//下拉列表，刷新数据
listView.on("pull", function(data) {
	if (data.state == 2) {
		laodPage();
	}
});

// 上拉列表，翻页数据
listView.on("push", function(data) {
	if (isLoadFirst&&data.state == 2) {
		if(tabType==1){
			getNextPageCommunityMessage();
		}else if(tabType==2){
			getNextPageCircle();
		}
		listView.rebound();
	}
});

listView.on("scroll", function(data){
	topCellUi.visible = data.firstVisiblePosition > 0;
});

do_Page.on("community_newIndex_tab", function(data) {
	if(null!=data&&data.tabType=="circle"){
		tabType=2;
		clearData=true;
		getCircle();
	}else if(null!=data&&data.tabType=="communityMessage"){
		tabType=1;
		clearData=true;
		if(null!=data.tagId){
			tagId=data.tagId;
		}
		getCommunityMessage();
	}
});
//变换top的社区
do_Page.on("community_select_join", function(data) {
	do_Page.fire("popnavItem");
	laodPage();
});
//index点击社区
do_Page.on("index_community_tab_touch", function(data) {
	if (communityId == null || communityId != communityUtil.communityVisitId()||data.brush) {
		laodPage();
	}
});
do_Page.on("index_community_sign_brush", function(data) {
	if(data.signCommunity==1){
		do_Page.fire("refreshSignState",{show:false});
	}
});

//点击加入社区或者签到
do_Page.on("community_join_sign", function(data) {
	var topData=listdata.getOne(0);
	topData.showCanJoin=false;
	topData.showCanSign=false;
	topData.showCanSigned=true;
	listdata.updateOne(0, topData);
	listView.refreshItems();
});

